/***
This do-file plots changes in employment by wage quartile and consumer spending,
restricting to the retail trade sector.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Employment"

* Create required subfolders
cap mkdir "${root}/results/Employment"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

* Erase output numbers
cap erase "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile and Consumer Spending, Retail Trade.yaml"

*-------------------------------------------------------------------------------
* Prepare employment data
*-------------------------------------------------------------------------------

* Set last date
local last_date = ${finaldate}
local last_short = mdy(3, 5, 2021)

* Use combined series
project, uses("${root}/data/web/data/Employment - National - Weekly.csv")
import delimited "${root}/data/web/data/Employment - National - Weekly.csv", clear

gen date = mdy(month, day_endofweek, year)

* Drop Extra Variables
keep date emp_retail_inclow emp_retail_inchigh

* Rescale
replace emp_retail_inchigh = emp_retail_inchigh * 100
replace emp_retail_inclow = emp_retail_inclow * 100

* Save
keep if inrange(date, mdy(2, 12, 2020), `last_date')
tempfile employment
save `employment'

*-------------------------------------------------------------------------------
* Prepare spending data
*-------------------------------------------------------------------------------

project, uses("${root}/data/web/data/Affinity - National - Daily.csv")
import delimited "${root}/data/web/data/Affinity - National - Daily.csv", clear
rename spend_retail_w_grocery norm_spend
replace norm_spend = 100 * norm_spend
gen date = mdy(month, day, year)
keep norm_spend date

gen Fridays = date - dow(date) + 5                                              
format Fridays %td
drop date
rename Fridays date

gcollapse norm_spend, by(date)                                              

* Confirm structure
isid date

*-------------------------------------------------------------------------------
* Plot
*-------------------------------------------------------------------------------

* Merge together
merge 1:m date using `employment', assert(1 3) keep(3) nogen
format date %td

* Choose last point of graph
local end_graph = `last_date' + 60

* Make annotations
sum emp_retail_inchigh if date == `last_date'
assert `r(N)' == 1
local end_q4 : di %3.1f `r(mean)'
local end_q4_str = cond(`end_q4' > 0, "+`end_q4'%", "`end_q4'%")
sum emp_retail_inclow if date == `last_date'
assert `r(N)' == 1
local end_q1 : di %3.1f `r(mean)'
local end_q1_str = cond(`end_q1' > 0, "+`end_q1'%", "`end_q1'%")
sum norm_spend if date == `last_date'
assert `r(N)' == 1
local end_spend : di %3.1f `r(mean)'
local end_spend_str = cond(`end_spend' > 0, "+`end_spend'%", "`end_spend'%")

sort date

tw ///
	(line emp_retail_inchigh date, color(oi2)) ///
	(line emp_retail_inclow date, color(oi1)) ///
	(line norm_spend date, color(oi6)) ///
	(scatter emp_retail_inchigh date if date == `last_date', color(oi2)) ///
	(scatter emp_retail_inclow date if date == `last_date', color(oi1)) ///
	(scatter norm_spend date if date == `last_date', color(oi6)) ///
	, ///
	legend(off) ///
	text(29 `=mdy(2, 1, 2021)' "Retail Consumer Spending", size(*0.92) color(oi6)) ///
	text(-6.5 `=mdy(2, 1, 2021)' "Retail Employment: Top Wage Quartile", size(*0.92) color(oi2)) ///
	text(-25 `=mdy(2, 1, 2021)' "Retail Employment: Bottom Wage Quartile", size(*0.92) color(oi1)) ///
	xline(`last_date', lcolor(gs12)) ///
	text(34 `=`last_date' + 50' "December 31 2021", place(9) color(gs7) size(2.5)) ///
	xtitle("") ///
	xlab(`=mdy(1, 1, 2020)' `""Jan" "2020""'  `=mdy(3, 1, 2020)' "Mar" ///
	 `=mdy(5, 1, 2020)' "May"  ///
	`=mdy(7, 1, 2020)' "Jul"  `=mdy(9, 1, 2020)' "Sep" ///
	 `=mdy(11, 1, 2020)' "Nov"  ///
	`=mdy(1, 1, 2021)' `""Jan" "2021""'   `=mdy(3, 1, 2021)' "Mar" ///
	 `=mdy(5, 1, 2021)' "May"  ///
	`=mdy(7, 1, 2021)' "Jul"  `=mdy(9, 1, 2021)' "Sep" `=mdy(11, 1, 2021)' "Nov" ///
	, format(%tdm) labsize(small)) ///
	b1title("        ", color(white) pos(3) ring(5)) ///
	ytitle("Change in Employment (%)" "Relative to January 2020") ///
	yline(0, lpattern(dash) lcolor(gs8)) ///
	ylab(-40 "-40%"  -20 "-20%"   0 "0%"  20 "+20%", nogrid) yscale(range(-40 30)) ///
	text(`=`end_q4'' `=`last_date' + 45' "`end_q4_str'", color(oi2) size(medsmall)) ///
	text(`end_q1' `=`last_date' + 45' "`end_q1_str'", color(oi1 ) size(medsmall)) ///
	text(`end_spend' `=`last_date' + 45' "`end_spend_str'", color(oi6) size(medsmall)) ///
	${title_`version'}

oi_graph_export "${root}/results/Employment/Changes in Employment by Wage Quartile and Consumer Spending, Retail Trade - long", type(${fig_type})

*-------------------------------------------------------------------------------
* Output numbers
*-------------------------------------------------------------------------------

local end_spend = abs(`end_spend')
local end_q4 = abs(`end_q4')
local end_q1 = abs(`end_q1')

* Export output numbers to csv file
yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile and Consumer Spending, Retail Trade.yaml", ///
	key("spend_retail") ///
	comment("Change in Consumer Spending relative to January 2020") ///
	value(`end_spend') fmt(%2.0f)

yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile and Consumer Spending, Retail Trade.yaml", ///
	key("emp_retail_q4") ///
	comment("Change in Employment relative to January 2020 - Top Wage Quartile") ///
	value(`end_q4') fmt(%2.0f)

yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile and Consumer Spending, Retail Trade.yaml", ///
	key("emp_retail_q1") ///
	comment("Change in Employment relative to January 2020 - Bottom Wage Quartile") ///
	value(`end_q1') fmt(%2.0f)

project, creates("${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile and Consumer Spending, Retail Trade.yaml")
